package com.example.checkctf.View;

import androidx.annotation.NonNull;

import android.Manifest;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.example.baselibrary.Base.BaseActivity;
import com.example.baselibrary.Bean.Movie;


import com.example.baselibrary.Utils.Customer;
import com.example.baselibrary.Utils.FileUtils;
import com.example.baselibrary.Utils.PermissionsUtils;
import com.example.baselibrary.Utils.SPUtils;
import com.example.checkctf.Interface.ObserverOnNextListener;
import com.example.checkctf.R;

public class WelcomeActivity extends BaseActivity {

    private static final String TAG = "WelcomeActivity";
    String[] permissions = new String[]{Manifest.permission.CAMERA, Manifest.permission.INTERNET, Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.ACCESS_NETWORK_STATE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE};
    private Button logBt;
    private boolean isClick;

    Handler mhandler = new Handler() {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
                    if (!isClick) {
                        startActivity(new Intent(WelcomeActivity.this, MainActivity.class));
                    }
                    break;
            }

        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        ObserverOnNextListener<Movie> listener = new ObserverOnNextListener<Movie>() {
            @Override
            public void onNext(Movie movie) {


            }
        };

    }

    @Override
    protected int getView() {
        return R.layout.activity_welcome;
    }

    @Override
    public void init() {
        logBt = (Button) findViewById(R.id.bt_login);

        if (Build.VERSION.SDK_INT < 23) {//6.0才用动态权限
            SPUtils.put(WelcomeActivity.this, Customer.GET_PERMISSION, true);
        }
        //申请权限
        PermissionsUtils.getInstance().chekPermissions(this, permissions, permissionsResult);

        if ((boolean) SPUtils.get(WelcomeActivity.this, Customer.GET_PERMISSION, false)) {
            mhandler.sendEmptyMessageDelayed(0, 1000);
        }
        ;

        logBt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if ((boolean) SPUtils.get(WelcomeActivity.this, Customer.GET_PERMISSION, false)) {
                    isClick = true;
                    startActivity(new Intent(WelcomeActivity.this, MainActivity.class));
                } else {
                    Toast.makeText(WelcomeActivity.this, "请先开启权限，不然程序无法正确运行", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    //创建监听权限的接口对象
    PermissionsUtils.IPermissionsResult permissionsResult = new PermissionsUtils.IPermissionsResult() {
        @Override
        public void passPermissons() {
            SPUtils.put(WelcomeActivity.this, Customer.GET_PERMISSION, true);
            //权限全部获取到，可以读取文件夹，然后读取配置信息
            FileUtils.readFile(WelcomeActivity.this);
        }

        @Override
        public void forbitPermissons() {
//            finish();
            SPUtils.put(WelcomeActivity.this, Customer.GET_PERMISSION, false);
        }
    };

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        PermissionsUtils.getInstance().onRequestPermissionsResult(this, requestCode, permissions, grantResults);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (null != mhandler) {
            mhandler = null;
        }
    }
}
